<?php

namespace App\Service;

use App\Models\EwConfigs;

class ConfigsService extends BaseService
{
	public static function getList($params, $count = FALSE)
	{
		$where['limit'] = $params->page_num;
		$where['offset'] = ($params->p - 1)*$params->page_num;

		$query = EwConfigs::query();
		$query->columns(['id,key_name,val,create_time,update_time']);

		//通过 key_name
		if (isset($params->key_name) && $params->key_name)
			$query->andwhere("key_name LIKE :key_name:")->bind(['key_name' => $params->key_name]);
		
		//总条数
		if ($count)
			return $query->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$query->orderBy($sql);
		}

		//分页
		$query->limit($params->page_num,($params->p-1)*$params->page_num);

		return $query->execute();
	}

	public static function getInfo($id = 0)
	{
		return EwConfigs::findFirst($id);
	}

	public static function count($params = NULL)
	{
		return EwConfigs::count();
	}

	public static function getVal( $params)
	{
		switch ($params->name) {
			case 'web':
				$key_name = "web_hot_search";
				break;

			case 'house':
				$key_name = "house_hot_search";
				break;

			case 'login':
				$key_name = "login_three";
				break;

			default:
				$key_name = $params->name;
				break;
		}

		$info = EwConfigs::findFirst(['conditions'=>'key_name = "' . $key_name .'"']);

		if (!$info)
			return NULL;

		return json_decode($info->val);
	}
    
    //
	public static function getKeyVal( $key_name)
	{
		$info = EwConfigs::findFirst(['conditions'=>'key_name = "' . $key_name .'"']);
		if (!$info)
			return NULL;

		return json_decode($info->val);
	}    

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$data->val = json_encode($data->val);

		$row = new EwConfigs;
		if (isset($data->id) && $data->id)
			$row = EwConfigs::findFirst($data->id);
        else if (isset($data->key_name) && $data->key_name)
			$row = EwConfigs::findFirstByKeyName($data->key_name);

		return $row->save((array)$data);
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwConfigs::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}

	/**
	 * [tables description]
	 * @return [type] [description]
	 */
	public static function getTable($table = NULL)
	{
		$info = EwConfigs::findFirst(['conditions' => 'key_name = "tables"']);
		$tables = (array)json_decode($info->val);
		// $tables = [
		// 	1  => "ew_house",
		// 	2  => "ew_news",
		// 	3  => "ew_deco",
		// 	4  => "ew_data",
		// 	5  => "ew_comment",
		// 	6  => "ew_video",
		// 	7  => "ew_deco_comp",
		// 	8  => "ew_rent",
		// 	9  => "ew_ads",
		// 	10 => "ew_ads_pos",
		// 	11 => "ew_bill",
		// 	12 => "ew_cats",
		// 	13 => "ew_city",
		// 	14 => "ew_configs",
		// 	15 => "ew_dics",
		// 	16 => "ew_feedbacks",
		// 	17 => "ew_gain",
		// 	18 => "ew_live_recorde",
		// 	19 => "ew_member",
		// 	20 => "ew_member_collect",
		// 	21 => "ew_member_praise",
		// 	22 => "ew_member_subs",
		// 	23 => "ew_page",
		// 	24 => "ew_push",
		// 	25 => "ew_roles",
		// 	26 => "ew_log_sys",
		// 	27 => "ew_login3",
		// 	28 => "ew_member_auth",
		// 	29 => "ew_member_log",
		// 	30 => "ew_nodes",
		// 	31 => "ew_nodes_auth",
		// 	32 => "ew_sms",
		// 	33 => "ew_uploads",
		// 	34 => "ew_user",
		// ];
		//echo ConfigsService::tables(1);
		if ($table && is_numeric($table) && in_array($table, array_keys($tables))){
			return $tables[$table];
		}
		//echo ConfigsService::tables("house");
		if ($table && is_string($table) && in_array($table, array_values($tables))){
			$tables = array_flip($tables);
			return $tables[$table];
		}

		return $tables;
	}
}